//
//  SQOrderLowViewController.m
//  pai
//
//  Created by linjunjing on 14-11-19.
//  Copyright (c) 2014年 linjunjing. All rights reserved.
//

#import "SQOrderLowViewController.h"
#import "SQLowOrderView.h"
#import "SQLowSamePlaceBidViewController.h"
#import "SQLowWinnersList.h"

@interface SQOrderLowViewController ()

@end

@implementation SQOrderLowViewController
@synthesize scrollView = _scrollView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setTitle:@"最低价－订单详情"];
    
    [self showList];
}

-(void)setItemWithObject:(lowCellObject*)object image:(NSString*)imgUrl
{
    self.lowObject = object;
    self.imageUrl = imgUrl;
}

-(void)showList
{
    _scrollView = [[UIScrollView alloc]init];
    //[_scrollView setBackgroundColor:[UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:0.5]];
    _scrollView.frame = CGRectMake(0, self.MyNavigationBar.frame.origin.y+self.MyNavigationBar.frame.size.height, 320, [[UIScreen  mainScreen] bounds].size.height - 44 - self.MyNavigationBar.frame.origin.y-self.MyNavigationBar.frame.size.height);
    _scrollView.backgroundColor = [UIColor colorWithRed:240/255.0 green:240/255.0 blue:240/255.0 alpha:1];
    _scrollView.pagingEnabled = NO;
    _scrollView.delegate = self;
    _scrollView.bounces = YES;
    [self.view addSubview:_scrollView];
    
    _lowiew = [SQLowOrderView initmyview];
    _lowiew.frame = CGRectMake(0, 0, 320, 320);
    [_lowiew initWithObject:self.lowObject image:self.imageUrl];
    
    [_scrollView addSubview:_lowiew];
    
    if (self.lowObject.status == 2) {
        //进行中
        NSDate* now = [NSDate date];
        
        self.beginTime = self.lowObject.sttime;
        self.endTime = self.lowObject.edtime;
        self.serviceTime = [now timeIntervalSince1970];
        self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateTime:) userInfo:nil repeats:YES];
    }else{
        [_lowiew updateTime:@"倒计时结束"];
    }
    //1:等待2：进行中--撞价失败3：结束--撞价失败4.结束不是最低价失败 8：成功
    NSInteger type = 0;
    switch (self.lowObject.status) {
        case 1:
        {
            //未开始
            
            break;
        }
        case 2:
        {
            if (self.lowObject.hits >1) {
                type = 2;
            }else{
                type = 1;
            }
            break;
        }
        case 3:
        {
            //已结束
            if (self.lowObject.auctprice < self.lowObject.strikeprice) {
                type = 3;
            }else if(self.lowObject.auctprice > self.lowObject.strikeprice){
                type = 4;
            }else{
                type = 8;
            }
            break;
        }
        case 4:
        {
            //撤销
            
            break;
        }
        case 5:
        {
            //终止
            
            break;
        }
        case 9:
        {
            //已经结算
            if (self.lowObject.auctprice < self.lowObject.strikeprice) {
                type = 3;
            }else if(self.lowObject.auctprice > self.lowObject.strikeprice){
                type = 4;
            }else{
                type = 8;
            }
            break;
        }
        default:
            break;
    }
    switch (type) {
        case 1:
        {
             //等待
            
            break;
        }
        case 2:
        {
            //进行中--撞价失败
            //显示撞价列表
            UIButton* hitButton = [[UIButton alloc]initWithFrame:CGRectMake(80, _lowiew.frame.origin.y+_lowiew.frame.size.height, 160, 40)];
            hitButton.tag = 1000;
            [hitButton setBackgroundImage:[UIImage imageNamed:@"hit_btn"] forState:UIControlStateNormal];
            [hitButton addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
            [_scrollView addSubview:hitButton];
            _scrollView.contentSize = CGSizeMake(320,hitButton.frame.origin.y + hitButton.frame.size.height + 30);
            break;
        }
        case 3:
        {
            //结束--撞价失败
            //显示撞价列表、中标者
            SQLowWinnersList* winner = [SQLowWinnersList initmyview];
            winner.frame = CGRectMake(9, _lowiew.frame.origin.y+_lowiew.frame.size.height, 302, 100);
            winner.win_nameLabel.text = self.lowObject.userid;
            winner.win_priceLabel.text = [NSString stringWithFormat:@"%ld",(long)self.lowObject.strikeprice];
            [_scrollView addSubview:winner];
            
            //显示撞价列表
            UIButton* hitButton = [[UIButton alloc]initWithFrame:CGRectMake(80, winner.frame.origin.y+winner.frame.size.height, 160, 40)];
            hitButton.tag = 1000;
            [hitButton setBackgroundImage:[UIImage imageNamed:@"hit_btn"] forState:UIControlStateNormal];
            [hitButton addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
            [_scrollView addSubview:hitButton];
            _scrollView.contentSize = CGSizeMake(320,hitButton.frame.origin.y + hitButton.frame.size.height + 30);
            break;
        }
        case 4:
        {
            //结束不是最低价失败
            //显示中标者
            SQLowWinnersList* winner = [SQLowWinnersList initmyview];
            winner.frame = CGRectMake(9, _lowiew.frame.origin.y+_lowiew.frame.size.height, 302, 100);
            winner.win_nameLabel.text = self.lowObject.userid;
            winner.win_priceLabel.text = [NSString stringWithFormat:@"%ld",(long)self.lowObject.strikeprice];
            [_scrollView addSubview:winner];
            _scrollView.contentSize = CGSizeMake(320,winner.frame.origin.y + winner.frame.size.height + 30);
            break;
        }
        case 8:
        {
            //成功
            //显示设置地址按钮
            
            break;
        }
        default:
            break;
    }
}
-(void)btnClicked:(UIButton*)btn
{
    if (btn.tag == 1000) {
        SQLowSamePlaceBidViewController* lspbvc = [[SQLowSamePlaceBidViewController alloc]init];
        lspbvc.serialnum = self.lowObject.serialnum;
        lspbvc.auctprice = self.lowObject.auctprice;
        [self.navigationController pushViewController:lspbvc animated:YES];
    }
}
-(void)updateTime:(NSTimer*)timer
{
    self.serviceTime ++ ;
    NSInteger time = self.endTime - self.serviceTime;
    if (self.serviceTime > self.endTime)
    {
        time = 0;
        [self.timer invalidate];
        self.timer = nil;
    }
    [self showtime:time];
}

-(void)showtime:(NSInteger)time
{
    NSInteger hour = time/3600;
    time = time%3600;
    NSInteger min = time/60;
    time = time%60;
    NSInteger sec = time;
    NSMutableString* str = [[NSMutableString alloc]init];
    if (hour < 10) {
        [str appendString:[[NSString alloc]initWithFormat:@"0%ld:",(long)hour]];
    }
    else{
        [str appendString:[[NSString alloc]initWithFormat:@"%ld:",(long)hour]];
    }
    if (min < 10) {
        [str appendString:[[NSString alloc]initWithFormat:@"0%ld:",(long)min]];
    }
    else{
        [str appendString:[[NSString alloc]initWithFormat:@"%ld:",(long)min]];
    }
    if (sec < 10) {
        [str appendString:[[NSString alloc]initWithFormat:@"0%ld",(long)sec]];
    }
    else{
        [str appendString:[[NSString alloc]initWithFormat:@"%ld",(long)sec]];
    }
    [_lowiew updateTime:str];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)onActionBack:(UIButton*)button
{
    NSLog(@"onaction");
    [super onActionBack:button];
    if (self.timer) {
        [self.timer invalidate];
        self.timer = nil;
    }
}
@end
